package com.larry.isn;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.List;

public class LarryFileWriter {

	private BufferedWriter writer;

	private BufferedWriter out;

	public LarryFileWriter(File file) {
		this.init(file);
	}

	public void write(List<?> datas) throws Exception {
		Integer i = 0;
		try {
			for (Object object : datas) {
				i++;
				if (!i.equals(datas.size())) {
					out.write(object.toString() + "\n");
				} else {
					out.write(object.toString());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeWriter();
		}
	}

	private void init(File file) {
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			writer = new BufferedWriter(new FileWriter(file));
			out = new BufferedWriter(writer);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void closeWriter() throws Exception {
		out.close();
		writer.close();
	}
}
